﻿using System;
using PmSadis.WebFormsUi.ExpandedControls;
using PmSadis.Repositories.Abstract;
using System.Linq;
using DevExpress.Web.Data;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxGridLookup;
using System.Collections.Generic;

namespace PmSadis.WebFormsUi.Operator
{
    public partial class GenContracts : System.Web.UI.Page
    {
        private IListRepository<PmSadis.Models.GenContract> _repo=new Repositories.GenContractRepository();
        private IListRepository<PmSadis.Models.SubContract> _repoSubContracts=new Repositories.SubContractRepository();
        
        protected void gvGenContracts_Init(object sender, EventArgs e)
        {
            var grid=sender as ASPxGridView;
            grid.SetDefaultSettings();
        }

        public IQueryable<PmSadis.Models.GenContract> GetAll()
        {
            return _repo.All;
        }
        public void DeleteGenContract(int contractId)
        {
            _repo.Delete(contractId);
        }
        public int InsertGenContract(string name, string number)
        {
            var genContract = new PmSadis.Models.GenContract
            {
                ContractId=0,
                Name=name,
                Number=number
            };
            return (int)_repo.Edit(genContract);
        }
        public void UpdateGenContract(int contractId, DateTime dateCreate, string name, string number)
        {
            var genContract = new PmSadis.Models.GenContract
            {
                ContractId = contractId,
                DateCreate=dateCreate,
                Name = name,
                Number = number
            };
            _repo.Edit(genContract);
        }
        
        
        protected void gvgenContractsRow_Updating(object sender, ASPxDataUpdatingEventArgs e)
        {
            var grid = sender as ASPxGridView;
            var dateCreate = grid.GetRowValues(grid.EditingRowVisibleIndex, "DateCreate");
            e.NewValues["DateCreate"] = dateCreate;
        }

        protected void gvSubContracts_BeforePerformDataSelect(object sender, EventArgs e)
        {
            var grid = sender as ASPxGridView;
            Session["oper-expanded-gencontract-id"] = grid.GetMasterRowKeyValue();
            grid.SetDefaultSettings();
        }

        
        public IQueryable<PmSadis.Models.SubContract> GetContractsByGenContractId(int genContractId)
        {
            return _repo.All
                .FirstOrDefault(x => x.ContractId == genContractId)
                .SubContracts
                .AsQueryable();
        }
        public void BreakLinkWithGenContract(int contractId)
        {
            (_repoSubContracts as PmSadis.Repositories.SubContractRepository).BreakLinkWithGenContract(contractId);
        }
        public IQueryable<PmSadis.Models.SubContract> GetUnlikedSubContracts()
        {
            return _repoSubContracts.All.Where(x => x.GenContractId == null);
        }

        protected void btnLinkSubContracts_Click(object sender, EventArgs e)
        {
            var genContractId = Session["oper-expanded-gencontract-id"];
            var visibleIndex = gvGenContracts.FindVisibleIndexByKeyValue(genContractId);
            var lookup = gvGenContracts.FindDetailRowTemplateControl(visibleIndex, "lucUnlinkedSubContracts") as ASPxGridLookup;
            var grid = gvGenContracts.FindDetailRowTemplateControl(visibleIndex, "gvSubContracts") as ASPxGridView;
            var subContractsId = lookup.GridView.GetSelectedFieldValues("ContractId");
            var list = new List<int>();
            subContractsId.ForEach(x => {
                list.Add(Convert.ToInt32(x));
            });
            LinkSubContracts(list, genContractId, lookup, grid);
        }
        
        private void LinkSubContracts(List<int> contractsId, object genContractId, ASPxGridLookup lookup, ASPxGridView grid)
        {
            (_repoSubContracts as PmSadis.Repositories.SubContractRepository).LinkSubContracts(contractsId, Convert.ToInt32(genContractId));

            lookup.DataBind();
            grid.DataBind();
        }
    }
}